package com.tanhua.server.controller;

import com.tanhua.server.pojo.Answers;
import com.tanhua.server.vo.SoulReportVo;
import com.tanhua.server.service.SoulService;
import com.tanhua.server.vo.QuestionnaireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Li QiChao
 * @Description: 测灵魂 表现层
 * @Date: Created in 10:43 2021/1/17
 * @ModifiedBy:
 */
@RestController
@RequestMapping("testSoul")
public class SoulController {

    @Autowired
    private SoulService soulService;


    /**
     * 问卷列表
     *
     * @return ResponseEntity<List<QuestionnaireVo>>
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> queryQuestionnaireList() {
        try {
            List<QuestionnaireVo> list = this.soulService.queryQuestionnaireList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 提交问卷
     * @return ResponseEntity<Void>
     */
    @PostMapping
    public ResponseEntity<String> commitQuestionnaire(
            @RequestBody Map<String, List<Answers>> answers) {

        try {
            List<Answers> answersList = answers.get("answers");
            Long id = this.soulService.commitQuestionnaire(answersList);
            return ResponseEntity.ok(String.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看问卷结果报告
     * @param id
     * @return ResponseEntity<List<SoulReportVo>>
     */
    @GetMapping("/report/{id}")
    public ResponseEntity<SoulReportVo> getSoulReport(
                                @PathVariable("id") Long id){
        try {
            SoulReportVo soulReportVoList = this.soulService.getSoulReport(id);
            return ResponseEntity.ok(soulReportVoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
